package com.demo.spring_demo.controller.user;

import com.demo.spring_demo.mapper.UserInfoMapper;
import com.demo.spring_demo.mapper.UserMapper;
import com.demo.spring_demo.pojo.*;
import com.demo.spring_demo.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@ResponseBody
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Value("${server.port}")
    private String port;

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    Utils utils;

    /**
     * 1、用户注册 / 添加
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserAndUserInfo user) {

        //没有账号提供生成的账号，有就不提供
        if(user.getUsername() == null || user.getUsername().isEmpty()) {
            user.setUsername(utils.generateString());
        }
        user.setRegisterDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

        System.out.println(user);
        if (userMapper.addUser(user) > 0 && userInfoMapper.addUserInfo(user) > 0) {
            return new Result(200, user, "success！");
        }
        return new Result(500, "error");
    }

    /**
     * 2、查询回显个人信息
     *
     * @param username
     * @return
     */
    @GetMapping("/info/{username}")
    public Result getInfo(@PathVariable String username) {


        UserAndUserInfo userAndUserInfo = userMapper.getUserAndUserInfo(username);

        if (userAndUserInfo != null) {
            return new Result(200, userAndUserInfo, "查询信息成功!");
        }
        return new Result(500, "查询失败!");
    }

    /**
     * 3、修改用户信息
     *
     * @param userAndUserInfo
     * @return
     */
    @PostMapping("/updateInfo")
    public Result getInfo(@RequestBody UserAndUserInfo userAndUserInfo) {

        User updateUser = new User(userAndUserInfo.getUsername(), userAndUserInfo.getEmail(), userAndUserInfo.getPassword(), userAndUserInfo.getNickname());
        UserInfo updateUserInfo = new UserInfo(userAndUserInfo.getUsername(), userAndUserInfo.getInfo(), userAndUserInfo.getPhone(), userAndUserInfo.getCity(), userAndUserInfo.getBirthday());
        if (userMapper.updateUser(updateUser) > 0 && userInfoMapper.updateUserInfo(updateUserInfo) > 0) {
            return new Result(200, "成功修改!");
        }

        return new Result(500, "修改失败!");
    }

    /**
     * 4、,分页查询
     */
    @PostMapping("/pageSelect")
    public Result pageSelect(@RequestBody PageInfo<User> pageInfo) {

        logger.info("请求的服务器端口："+port);

        List<User> userList = null;
        int count = 0;
        if ((count = userMapper.getCount(pageInfo.getUser())) > 0 && (userList = userMapper.getUserList(pageInfo.getUser(), pageInfo.getPageIndex() * pageInfo.getPageSize(), pageInfo.getPageSize())) != null) {
            return new Result(200, new PageInfo<User>(pageInfo.getPageIndex(), pageInfo.getPageSize(), userList, count));
        } else {
            return new Result(500, "信息不存在！");
        }

    }

    /**
     * 5、忘记密码
     *
     * @param user
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updateUserPassword(@RequestBody User user) {
        if (user != null && userMapper.updatePassword(user) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"success");
    }

    /**
     * 6、获取添加用户的账号
     * @return
     */
    @GetMapping("/getUsername")
    public Result generateUsername(){
        String username = utils.generateString();
//        System.out.println("成功获取");
        //提供账号
        return new Result(200,username,"success");
    }

    /**
     * 7、修改用户
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        if(user != null && userMapper.updateUser(user) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"error");
    }

    /**
     * 8、删除用户
     * @param
     * @return
     */
    @GetMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable int id){
        if(id > 0 && userMapper.deleteUser(id) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"error");
    }
}